package com.ziyia.a2_5_1;

import androidx.annotation.IntRange;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DialogTitle;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    // 创建一个BaseExpandableListAdapter对象
    BaseExpandableListAdapter adapter = new BaseExpandableListAdapter()
    {

        class GroupHolder{
            TextView title;
            public GroupHolder(TextView title) {
                this.title = title;
            }
        }

        class ChildHolder {
            TextView text;
            public ChildHolder(TextView text) {
                this.text = text;
            }
        }


        String[] armTypes = {"管理员用户", "普通用户", "来宾用户"};
        String[][] arms = {
                {"root", "Administrator", "admin"},
                {"bt_user", "ssh_user", "mysql_user", "ftp_user"},
                {"test", "jx", "123456"}
        };

        @Override
        public int getGroupCount()
        {
            return armTypes.length;
        }

        @Override
        public int getChildrenCount(int groupPosition)
        {
            return arms[groupPosition].length;
        }

        // 获取指定组位置处的组数据
        @Override
        public Object getGroup(int groupPosition)
        {
            return armTypes[groupPosition];
        }

        // 获取指定组位置、指定子列表项处的子列表项数据
        @Override
        public Object getChild(int groupPosition, int childPosition)
        {
            return arms[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition)
        {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        @Override
        public boolean hasStableIds()
        {
            return true;
        }

        // 该方法决定每个组选项的外观
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
        {
            GroupHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.group_item, null);
                TextView title = convertView.findViewById(R.id.child_title);
                viewHolder = new GroupHolder(title);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (GroupHolder) convertView.getTag();
            }
            viewHolder.title.setText(getGroup(groupPosition).toString());

            return convertView;
        }

        // 该方法决定每个子选项的外观
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
        {
            ChildHolder childHolder;
            if (convertView == null) {
                 convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.child_item, null);
                TextView text = convertView.findViewById(R.id.text);
                childHolder = new ChildHolder(text);
                convertView.setTag(childHolder);
            } else {
                childHolder = (ChildHolder) convertView.getTag();
            }
            String text = (String) getChild(groupPosition, childPosition);
            childHolder.text.setText(text);
            return convertView;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExpandableListView expandListView = findViewById(R.id.expandable);
        expandListView.setAdapter(adapter);



    }

}
